# -*- coding: utf-8 -*-
###############################################################################
# Import Modules
###############################################################################
import sys
from backtracking import backtracking
from galeshapley import galeshapley


def compareResult(filename, resultStr):
    outputFile = file(filename)
    outputStr = outputFile.read()
    if outputStr == resultStr:
        print "Sucess! The result matches the file contents for:", filename
    else:
        print "Fail! The result doesn't match the file contents for:", filename


def errormsg():
    print "Error. Script usage:"
    print "   $ python tdatp1.py [-gs|-bt] [inputFilename] [outputFilename]"


def main():
    if len(sys.argv) == 4:
        algorithm = sys.argv[1]
        inputFilename = sys.argv[2]
        outputFilename = sys.argv[3]

        resultStr = ''

        if algorithm == '-gs':
            print "------------------------------------"
            print "Gale & Shapley"
            print "------------------------------------"
            gs = galeshapley.GaleShapley(inputFilename)
            gs.match()
            resultStr = gs.toString()
        elif algorithm == '-bt':
            print "------------------------------------"
            print "Backtracking"
            print "------------------------------------"
            b = backtracking.Backtracking(inputFilename)
            b.match()
            resultStr = b.solution
        else:
            errormsg()

        if resultStr != '':
            print resultStr
            compareResult(outputFilename, resultStr)
    else:
        errormsg()


if __name__ == '__main__':
    main()
